package com.wsoft.utils;

import com.wsoft.constant.SensitiveConstant;
import org.springframework.stereotype.Component;

/**
 * @Author Zhang gukai
 * @Date 2024/11/12 8:57
 */
@Component
public class SensitiveDataUtil {
    /**
     * 对姓名进行脱敏
     *
     * @param name 姓名
     * @return 脱敏后的姓名
     */
    public String maskName(String name) {
        if (name == null || name.length() <= 1) {
            return name;
        }
        return name.charAt(0) + "**";
    }

    /**
     * 对手机号进行脱敏
     *
     * @param phoneNumber 手机号
     * @return 脱敏后的手机号
     */
    public String maskPhoneNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.length() != SensitiveConstant.PHONE_LENGTH) {
            return phoneNumber;
        }
        return phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
    }

    /**
     * 对身份证号进行脱敏
     *
     * @param idCard 身份证号
     * @return 脱敏后的身份证号
     */
    public String maskIdCard(String idCard) {
        if (idCard == null || idCard.length() != SensitiveConstant.ID_CARD_LENGTH) {
            return idCard;
        }
        return idCard.substring(0, 6) + "********" + idCard.substring(14);
    }

    /**
     * 对邮箱进行脱敏
     *
     * @param email 邮箱
     * @return 脱敏后的邮箱
     */
    public String maskEmail(String email) {
        if (email == null || !email.contains(SensitiveConstant.EMAIL)) {
            return email;
        }
        String[] parts = email.split("@");
        if (parts[0].length() <= SensitiveConstant.EMAIL_LENGTH) {
            return parts[0] + "***@" + parts[1];
        }
        return parts[0].substring(0, 3) + "***@" + parts[1];
    }
}
